package com.thread.singleton;

/**
 * 双检测单例
 * 线程安全
 */
public class DoubleCheckSingletonDemo {

    private volatile static DoubleCheckSingletonDemo doubleCheckSingletonDemo ;

    private DoubleCheckSingletonDemo() {
    }

    public DoubleCheckSingletonDemo getInstance(){
        if (doubleCheckSingletonDemo == null) {
            synchronized (DoubleCheckSingletonDemo.class) {
                if (doubleCheckSingletonDemo == null) {
                    //新建开辟堆内存空间，调用构造器和指向栈内存引用非原子操作，因此要加volatile
                    doubleCheckSingletonDemo = new DoubleCheckSingletonDemo();
                }
            }
        }
        return doubleCheckSingletonDemo;
    }
}
